Using Git and Github
April 15, 2025
Compiled software are a bunch of files. At each software improvment, developers want the software to be stable with no regression. For that, they have to know what files have changed between snapshots.
add & commitadd & commitadd & commitadd & commitadd & commitadd & commitbranchesbranchesmain par défault): c’est un série de commentaires (commit)commit) est ce que l’on appelle la tête de la branche (HEAD), elle contient la version la plus à jour des fichiers.commit) est attachée une version des fichiers.local vs remotegit init)git add)git commit)git fetch & git push)git fetch & git pull)git —versiongit configFor more details: https://happygitwithr.com/install-git
In order to reduce the learning curve, we will learn the R Studio way of interacting with git
So many other ways to interact with git:
gert packageAll the key concepts and process still the same!
To use git in Rstudio, you need to create a new Project. Let’s start by creating our project directory.
Open RStudio, in the top right corner create a new project
You can now look under the git tab, you should see this:
Create a new file: go to the menu File => New file => R Script. Make sure to save it with the name script_1.R.
What happens in the git tab?
This is as simple as “checking off” the file in the Git tab. This is called Staging the file.
What is a gitignore? A gitignore is a file that lists the file you never want git to track. It can match certain file names (for instance, .csv or .tif files). This can be useful in case you need to make sure certain files (like data files or large files), do not get added.
Click on commit on the top of the file list. This window should appear:
Before committing anything you need to add a commit message. It is important to add a useful message to your commit, a bit like a journal entry, so that you can remember what you committed.
Click on commit in order to commit the changes!
In the first workshop, we learned the basics of Git. We covered:
.gitignore file (relevant for sensitive data)git Workflow Overview ReminderThe typical workflow:
Tip
We can make an analogy with taking a family picture, where each family member would represent a file.
Staging files is like deciding which family member(s) are going to be on your next picture Committing is like taking the picture
Étape 1. Pour ceux qui n’ont pas de compte, s’inscrire sur Github en suivant le lien
Étape 2. Configurer ses credentials pour que Github nous reconnaisse
We have to store a token in our local system. This token is used to authenticate your computer with Github.
Tip
Token = password
Étape 2. Configurer ses credentials pour que Github nous reconnaisse
Run the following command in RStudio
ghp_Étape 2. Configurer ses credentials pour que Github nous reconnaisse
Run the following command in RStudio
This command will open a prompt in RStudio
Paste the token you copied in the previous step
Étape 2. Configurer ses credentials pour que Github nous reconnaisse
Assess if everything is fine and the credentials are OK
Note
This way, you will not have to enter your credentials every time you push or pull from GitHub. If you want to know more on this authentification process, have a look at the usethis documentation
Étape 3. Connect your local repository to Github
Note
usethis::use_github(private = TRUE) if you want your repository to be private, because you are working with sensitive data
The configuration is done and the local is now sync! Lets have a quick look at the Github repo on the website.
Lets go back on RStudio and check the Git tab. Two new buttons Pull and Push are now available.
Commit, move a changed local file to your local staging areaPull, get file(s) from the cloud to your local computer – opposite of a “push”Push, move file(s) to the cloud from your local computer – opposite of a “pull”You have to repeat step 2-4, every time you edit, add, delete a new file if you want to keep your local and remote repository in sync.